﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Gx.Common
{
    public class LoginHelper
    {

        public static string UserCookie = ConfigHelper.GetWebConfig("UserCookie");

        public static string ExitLogin = ConfigHelper.GetWebConfig("ExitLogin");
        /// <summary>
        /// 得到Cooie解密
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string GetCookieDecrypt(string key)
        {
            string cookieKey = CookieHelper.GetCookie(UserCookie,
                  HttpUtility.UrlEncode(Base64Helper.Encryption(key, ConfigHelper.CookieSecretKey)));
            return Base64Helper.Decrypt(HttpUtility.UrlDecode(cookieKey), ConfigHelper.CookieSecretKey);
        }

        /// <summary>
        /// 加密
        /// </summary>
        /// <returns></returns>
        public static string GetCookieEncryption(string key)
        {
            return HttpUtility.UrlEncode(Base64Helper.Encryption(key, ConfigHelper.CookieSecretKey));
        }


        /// <summary>
        /// 得到用户ID
        /// </summary>
        /// <returns></returns>
        public static int GetUserId()
        {
            return int.Parse(GetCookieDecrypt(GlobalHelper.COOKIEUSERID));
        }


        /// <summary>
        /// 得到用户姓名
        /// </summary>
        /// <returns></returns>
        public static string UserName()
        {
            return GetCookieDecrypt(GlobalHelper.COOKIEUSERNAME);
        }


        /// <summary>
        /// 获取角色id
        /// </summary>
        /// <returns></returns>
        public static int GetRoleId()
        {
            return int.Parse(GetCookieDecrypt(GlobalHelper.COOKIEROLEID));
        }
        
    }
}